package com.rainbow.common.handler.auth;

import com.rainbow.common.core.constant.Constant;
import com.rainbow.common.core.toolkit.ContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 国际化配置
 * @author 傅为地
 */
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
public class I18nInterceptor implements HandlerInterceptor {

    @Override
    @Trace
    public boolean preHandle(@Nonnull HttpServletRequest request, @Nonnull HttpServletResponse response, @Nonnull Object handler) {
        //国际化i18n处理
        ContextHolder.getInstance().put(Constant.GLOBAL_MESSAGE_I18N_KEY,request.getLocale());
        return true;
    }

    @Override
    public void afterCompletion(@Nonnull HttpServletRequest request, @Nonnull HttpServletResponse response, @Nonnull Object object, Exception exception) {
        //国际化i18n处理
        ContextHolder.getInstance().remove(Constant.GLOBAL_MESSAGE_I18N_KEY);
    }

}
